<?php

  require_once('db_connection.php');

  $table = 'users';

  $desc = "DESCRIBE `$table`";
  $descResult = mysql_query($desc);

  while($colData = mysql_fetch_assoc($descResult)) {
    //print_r($colData);
    $cols[] = $colData['Field'];
  }
  print_r($cols);


  $fp = fopen("{$table}_data.csv", 'w');

  $dataArray = fetchAll($table);

  $cols = array_map('strtoupper',$cols);
  $cols = array_map('underscoreToSpace',$cols);
  # Create Header
  fputcsv($fp, $cols, ',');

  while ($fields = mysql_fetch_assoc($dataArray)) {
    //print_r($fields);
      fputcsv($fp, $fields, ',');
  }

  fclose($fp);

  function underscoreToSpace($str)
  {
    return str_replace('_',' ',$str);
  }
